package levelSystem.levels;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import tools.Constants;
import tools.ImageHandler;

import levelSystem.BaseMenuLevel;

import javax.sound.midi.*;

public class MainMenu extends BaseMenuLevel{

    private int curSelection;
    private boolean selected;
    
    private BufferedImage newGameButton;
    //private BufferedImage loadGameButton;
    private BufferedImage quitButton;

    private boolean upKeyPressed;
    private boolean downKeyPressed;
    
    //private boolean mute;
        
    public MainMenu() {
    	super();
        curSelection = 1;
        selected = false;
        prevLevel = null; // main menu nao tem level anterior
        
        player = null;
        song = null;  
        
        songThread = null;
        
        canPause = false;
    }
    private void newGame()
    {
    	if(fadeFinished) 
    	{
    		player.stop();
    		player.close();
    		levelHandler.changeLevel(new NewGame());
    	}
    }
//    private void loadGame()
//    {
//    	if(fadeFinished) {
//    		player.stop();
//    		player.close();
//    		levelHandler.changeLevel(new LoadGame(this));
//    	}
//    }
    
    @Override
    public boolean logic() {
    	if(paused && player.isOpen()) player.stop();
    	else if(player.isOpen() && !player.isRunning()) player.start();
    	procOption();
        return true;
    }
    
    @Override
    public void renderLevel(Graphics gg) {
    	
    	BufferedImage aux = new BufferedImage(13*Constants.TILE_WIDTH, 10*Constants.TILE_HEIGHT, BufferedImage.TRANSLUCENT);
    	Graphics g = aux.getGraphics();
    	
    	if(background != null && newGameButton != null && /*loadGameButton != null &&*/ cursor != null && quitButton != null)
    	{
	    	g.drawImage(background, 0, 0, null);
	    	
	    	g.drawImage(newGameButton, aux.getWidth() - Constants.MENU_BUTTON_WIDTH - 10 , 
	    			(aux.getHeight()-Constants.MENU_BUTTON_HEIGHT)/2 + 20, null);
	    	/*g.drawImage(loadGameButton, aux.getWidth() - Constants.MENU_BUTTON_WIDTH - 10, 
	    			(aux.getHeight()-Constants.MENU_BUTTON_HEIGHT)/2 + 40, null);*/
	    	g.drawImage(quitButton, aux.getWidth() - Constants.MENU_BUTTON_WIDTH - 10, 
	    			(aux.getHeight()-Constants.MENU_BUTTON_HEIGHT)/2 + 70, null);
	    	
	    	g.drawImage(cursor, aux.getWidth()-Constants.MENU_BUTTON_WIDTH - cursor.getWidth() - 10, 
	    			(aux.getHeight()-Constants.MENU_BUTTON_HEIGHT)/2 + 20 +(curSelection-1)*50, null);
    	}
    	
    	if(!fadeFinished)
    	{
    		if(fadeType == 1)
    		{
    			fadeIn(g, aux.getWidth(), aux.getHeight());
    		}else fadeOut(g, aux.getWidth(), aux.getHeight());
    	}
    	
    	gg.drawImage(aux, 0, 0, Constants.DEFAULT_SCREEN_WIDTH, Constants.DEFAULT_SCREEN_HEIGHT, null);
    	
    	g.dispose();
    	gg.dispose();
    }

    @Override
    protected void procUp(int i) {
    	if(!selected)
    	{
	    	if(!upKeyPressed && !downKeyPressed)
	    	{
	    		upKeyPressed = true;
		    	curSelection--;
		    	if(curSelection < 1) curSelection = 2;
	    	}else if(i == 2)
	    	{
	    		upKeyPressed = false;
	    	}
    	}
    }

    @Override
    protected void procDown(int i) {
    	if(!selected)
    	{
	    	if(!upKeyPressed && !downKeyPressed)
	    	{
	    		downKeyPressed = true;
		    	curSelection++;
		    	if(curSelection > 2) curSelection = 1;
	    	}else if(i == 2)
	    	{
	    		downKeyPressed = false;
	    	}
    	}
    }

    @Override
    protected void procLeft(int i) {
    	procUp(i);
    }

    @Override
    protected void procRight(int i) {
    	procD(i);
    }

    @Override
    protected void procA(int i) {
    	selected = true;
    	setFadeOutComponents();
    }

    @Override
    protected void procS(int i) {} // nao utilizado
    @Override
    protected void procD(int i) {} // nao utilizado
    @Override
    protected void procW(int i) {} // nao utilizado

    @Override
    protected void procEnter(int i) {
    	procA(i);
    }
	@Override
	protected void showDebug(Graphics g) {}
	@Override
	public void init() {
		String path = "/img/elements/";
		
		cursor = ImageHandler.loadImage(path+"cursor.png");
		cursor = ImageHandler.addColorKey(cursor);
		
		background = ImageHandler.loadImage(path+"mainMenuBg.png");
		
		newGameButton = ImageHandler.loadImage(path+"newGameButton.png");
		newGameButton = ImageHandler.addColorKey(newGameButton);
		
		//loadGameButton = ImageHandler.loadImage(path+"loadGameButton.png");
		//loadGameButton = ImageHandler.addColorKey(loadGameButton);
		
		quitButton = ImageHandler.loadImage(path+"quitButton.png");
		quitButton = ImageHandler.addColorKey(quitButton);
		
		if(songThread == null)
		{
			songThread = new Thread(songRun);
		}
		
		InputStream is = MainMenu.class.getResourceAsStream("/sound/mainMenuSong.mid");
        if(is == null)
        {
        	System.out.println("som nao encontrado");
        }else
        {
	        try {
				song = MidiSystem.getSequence(is);
				player = MidiSystem.getSequencer();
			} catch (InvalidMidiDataException e) {
				e.printStackTrace();
			} catch ( IOException e )
			{
				e.printStackTrace();
			} catch (MidiUnavailableException e) {
				e.printStackTrace();
			}
        }
        
        songThread.start();
	}
	
	@Override
	protected void procOption() {
		if(selected)
        {
            switch(curSelection)
            {
                case 1:
                    newGame();
                    break;
                /*case 2:
                    loadGame();
                    break;*/
                case 2:
                	levelHandler.quit();
                	break;
            }
        }
	}
	@Override
	public void init(int x, int y) {
		init();
		
	}
}